package com.luomo.wenyuan.modules.wenyuan.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.luomo.wenyuan.common.utils.PageUtils;
import com.luomo.wenyuan.common.utils.R;
import com.luomo.wenyuan.modules.wenyuan.dao.TDeliveryDao;
import com.luomo.wenyuan.modules.wenyuan.entity.*;
import com.luomo.wenyuan.modules.wenyuan.service.*;
import com.luomo.wenyuan.modules.wenyuan.vo.TBookDetailVo;
import com.luomo.wenyuan.modules.wenyuan.vo.TDeliveryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * @author
 * @email
 * @date 2020-10-19 15:58:39
 */
@RestController
@RequestMapping("generator/tdelivery")
public class TDeliveryController {
    @Autowired
    private TDeliveryService tDeliveryService;

    @Autowired
    private TStoreService tStoreService;

    @Autowired
    private TBookService tBookService;

    @Autowired
    private TBookDetailService tBookDetailService;

    @Autowired
    private TPutwayApplicationService tPutwayApplicationService;

    @Autowired
    private TPutawayBookService tPutawayBookService;

    @Autowired
    private TDeliveryDao tDeliveryDao;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = tDeliveryService.queryPage(params);
        List<TDeliveryEntity> list = (List<TDeliveryEntity>) page.getList();
        if (CollectionUtil.isNotEmpty(list)) {
            List<TDeliveryVo> tDeliveryVos = new ArrayList<>();
            TDeliveryVo tDeliveryVo;
            for (TDeliveryEntity tDeliveryEntity : list) {
                tDeliveryVo = new TDeliveryVo();
                BeanUtil.copyProperties(tDeliveryEntity, tDeliveryVo);
                Integer putawayId = tDeliveryEntity.getPutawayId();
                List<TPutawayBookEntity> deliveryBooks = tPutawayBookService.getByPutawayId(putawayId);
                List<TBookDetailVo> tBookDetailVos = new ArrayList<>();
                TBookDetailVo tBookDetailVo;
                Set<String> bookNameSet = new HashSet<>();
                Set<String> storeNameSet = new HashSet<>();
                for (TPutawayBookEntity deliveryBook : deliveryBooks) {
                    Integer bookDetailId = deliveryBook.getBookDetailId();
                    TBookDetailEntity bookDetail = tBookDetailService.getById(bookDetailId);
                    Integer bookId = bookDetail.getBookId();
                    TBookEntity book = tBookService.getById(bookId);
                    bookNameSet.add(book.getName());
                    Integer storeId = bookDetail.getStoreId();
                    TStoreEntity store = tStoreService.getById(storeId);
                    storeNameSet.add(store.getName());
                    tBookDetailVo = new TBookDetailVo();
                    BeanUtil.copyProperties(book, tBookDetailVo);
                    tBookDetailVo.setDetailId(bookDetailId);
                    tBookDetailVo.setStatus(bookDetail.getStatus());
                    tBookDetailVo.setStoreDate(bookDetail.getStoreDate());
                    tBookDetailVo.setStoreName(store.getName());
                    tBookDetailVos.add(tBookDetailVo);
                }
                tDeliveryVo.setBookList(tBookDetailVos);
                tDeliveryVo.setBookNameList(StrUtil.join(",", bookNameSet));
                tDeliveryVo.setStoreNameList(StrUtil.join(",", storeNameSet));
                tDeliveryVos.add(tDeliveryVo);
            }
            page.setList(tDeliveryVos);
        }
        return R.ok().put("page", page);
    }

    @RequestMapping("/putaway/{id}")
    @Transactional(rollbackFor = Exception.class)
    public R putaway(@PathVariable("id") Integer id) {
        TDeliveryEntity deliveryEntity = tDeliveryService.getById(id);
        deliveryEntity.setStatus(2);
        tDeliveryService.updateById(deliveryEntity);
        Integer putawayId = deliveryEntity.getPutawayId();
        List<TPutawayBookEntity> deliveryBooks = tPutawayBookService.getByPutawayId(putawayId);
        for (TPutawayBookEntity deliveryBook : deliveryBooks) {
            Integer bookDetailId = deliveryBook.getBookDetailId();
            TBookDetailEntity bookDetailEntity = tBookDetailService.getById(bookDetailId);
            bookDetailEntity.setStatus(2);
            tBookDetailService.updateById(bookDetailEntity);
        }
        UpdateWrapper<TPutwayApplicationEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", putawayId);
        updateWrapper.set("status", 3);
        tPutwayApplicationService.update(updateWrapper);
        return R.ok();
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{putwayId}")
    public R info(@PathVariable("putwayId") Integer putwayId) {
        TDeliveryEntity tDelivery = tDeliveryDao.getByPutwayId(putwayId);
        TDeliveryVo tDeliveryVo = new TDeliveryVo();
        BeanUtil.copyProperties(tDelivery, tDeliveryVo);
        Integer putawayId = tDelivery.getPutawayId();
        List<TPutawayBookEntity> deliveryBooks = tPutawayBookService.getByPutawayId(putawayId);
        List<TBookDetailVo> tBookDetailVos = new ArrayList<>();
        TBookDetailVo tBookDetailVo;
        Set<String> bookNameSet = new HashSet<>();
        Set<String> storeNameSet = new HashSet<>();
        for (TPutawayBookEntity deliveryBook : deliveryBooks) {
            Integer bookDetailId = deliveryBook.getBookDetailId();
            TBookDetailEntity bookDetail = tBookDetailService.getById(bookDetailId);
            Integer bookId = bookDetail.getBookId();
            TBookEntity book = tBookService.getById(bookId);
            bookNameSet.add(book.getName());
            Integer storeId = bookDetail.getStoreId();
            TStoreEntity store = tStoreService.getById(storeId);
            storeNameSet.add(store.getName());
            tBookDetailVo = new TBookDetailVo();
            BeanUtil.copyProperties(book, tBookDetailVo);
            tBookDetailVo.setDetailId(bookDetailId);
            tBookDetailVo.setStatus(bookDetail.getStatus());
            tBookDetailVo.setStoreDate(bookDetail.getStoreDate());
            tBookDetailVo.setStoreName(store.getName());
            tBookDetailVos.add(tBookDetailVo);
        }
        tDeliveryVo.setBookList(tBookDetailVos);
        tDeliveryVo.setBookNameList(StrUtil.join(",", bookNameSet));
        tDeliveryVo.setStoreNameList(StrUtil.join(",", storeNameSet));
        return R.ok().put("data", tDeliveryVo);
    }


    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody TDeliveryEntity tDelivery) {
        tDeliveryService.updateById(tDelivery);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids) {
        tDeliveryService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
